package reserve

import (
    "time"
	"strconv"
	"dhfshop/controllers/base"
	"dhfshop/models/reserve"
	//"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
)

type SchemaController struct {
	base.BaseController
}

func (this *SchemaController) List() {
	curRoleId := this.Ctx.Input.GetData("curRoleId").(int)
	partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	query,_, fields, order_by, offset, limit, err := this.GetQueryPara()
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	if curRoleId == 2 {
	    query["partner_id"] = strconv.FormatInt(partnerId, 10)
	}
	var allowStoreId []int64
	if this.CurrentUser != nil {
	    allowStoreId = this.CurrentUser.AllowStoreId
	}
	//beego.Debug("allowStoreId:", allowStoreId)
	l, cnt, err := reserve.GetAllSchemas(allowStoreId,query, fields, order_by, offset, limit)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	r := struct {
		TotalCount int64             `json:"total_count"`
		Schemas    []*reserve.Schema `json:"schemas"`
	}{TotalCount: cnt, Schemas: l}
	this.Data["json"] = r
	this.ServeJSON()
}

func (this *SchemaController) Post() {
	var v reserve.Schema
	this.ParseForm(&v)
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	if schemaType, err := reserve.GetSchemaTypeById(v.SchemaTypeId); err != nil {
		this.ServeError(err.Error())
		return
	} else {
		v.SchemaType = schemaType
	}
	id, err := reserve.AddSchema(&v)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	v.SchemaId = int64(id)
	rsp := struct {
		SchemaId  int64     `json:"schema_id"`
		Modified time.Time `json:"modified"`
	}{SchemaId: id, Modified: time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *SchemaController) Update() {
	var v reserve.Schema
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.SchemaId, "schema_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	if v.SchemaTypeId > 0 {
	    if schemaType, err := reserve.GetSchemaTypeById(v.SchemaTypeId); err != nil {
			this.ServeError(err.Error())
			return
		} else {
			v.SchemaType = schemaType
		}
	}
	updateFields, err := this.GetUpdateFields(v, "", "form", []string{"schema_id"})
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	if len(updateFields) > 0 {
		err := reserve.UpdateSchemaById(&v, updateFields...)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
	}
	rsp := struct {
		SchemaId  int64     `json:"schema_id"`
		Modified time.Time `json:"modified"`
	}{SchemaId: v.SchemaId, Modified: time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *SchemaController) Delete() {
	var v reserve.Schema
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.SchemaId, "schema_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	err := reserve.DeleteSchema(v.SchemaId)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	r := struct {
		SchemaId  int64     `json:"schema_id"`
		Modified time.Time `json:"modified"`
	}{SchemaId: v.SchemaId, Modified: time.Now().UTC()}
	this.Data["json"] = r
	this.ServeJSON()
}

